<template>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="渠道" prop="source">
            <select-app-source v-model="form.source"></select-app-source>
        </el-form-item>
        <el-form-item label="母包" prop="base_apk">
            <el-select v-model="form.base_apk" clearable class="filter-item" placeholder="母包" style="width: 300px;">
                <el-option
                    v-for="(item, index) in baseList"
                    :key="index"
                    :label="item.name"
                    :value="item.name">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" :loading="loading" @click="submit">确定</el-button>
            <el-button @click="cancel">返回</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        props: {
            data: {

            }
        },
        data() {
            return {
                loading: false,
                form: {},
                formDefault: {
                    id: 0,
                    // appid: '',
                    source: '',
                    name: '',
                    base_apk: '',
                    // domain_group_name: '',
                },
                rules: {
                    // appid: [
                    //     { required: true, message: '请选择关联应用', trigger: 'blur' },
                    // ],
                    source: [
                        { required: true, message: '请选择关联渠道', trigger: 'blur' },
                    ],
                    // domain_group_name: [
                    //    { required: true, message: '请选择域名组', trigger: 'blur' },
                    // ],
                    base_apk: [
                        { required: true, message: '请选择关联母包', trigger: 'blur' },
                    ],
                },
            }
        },
        computed: {
            baseList() {
                return this.$store.state.apk.baseList;
            },
            // domainGroups() {
            //     return this.$store.state.domainGroup.all.map(v => {
            //         return {
            //             label: v.name,
            //             value: v.name,
            //         }
            //     })
            // }
        },
        watch: {
            data (newVal) {
                this.syncForm();
            }
        },
        methods: {
            getFormAction() {
                return this.form.id ? 'apk/Update' : 'apk/Store';
            },
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return false;
                    this.loading = true;
                    this.$store.dispatch(this.getFormAction(), this.form).then(res => {
                        this.$message.success(res.data.message);
                        this.$emit('submit');
                    }).finally(() => {
                        this.loading = false;
                    });
                });
            },
            cancel() {
                this.$emit('cancel');
            },
            syncForm() {
                this.form = Object.assign({}, this.data.id ? this.data : this.formDefault);
            },
            getBaseList() {
                if (!this.baseList.length) this.$store.dispatch('apk/BaseList');
            },
            // getDomainGroups() {
            //     if (!this.domainGroups.length) this.$store.dispatch('domainGroup/All');
            // }
        },
        created () {
            this.syncForm();

            this.getBaseList();
            // this.getDomainGroups();
        }
    }
</script>

<style lang="scss" scoped>
.item-row {
    margin: 0 0 10px 0;
}
</style>

